package es.udc.emarketapp.http.controller.frontcontroller;

import es.udc.emarketapp.http.controller.session.SessionManager;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 * A filter to check if the action to be executed requires that the user had 
 * been authenticated. If the user has not been authenticated and the action 
 * requires it, <code>doProcess</code> returns the <code>ActionForward</code>
 * returned by <code>mapping.findForward("AuthenticationPage")</code>.
 */
public class ClientPreProcessingFilter extends PreProcessingFilter {

    public ClientPreProcessingFilter(PreProcessingFilter nextFilter) {
        super(nextFilter);
    }

    protected ActionForward doProcess(HttpServletRequest request,
            HttpServletResponse response, Action action, ActionForm form,
            ActionMapping mapping) throws IOException, ServletException,
            InternalErrorException {

        EMarketActionMapping eMarketActionMapping =
                (EMarketActionMapping) mapping;

        if (eMarketActionMapping.isClientRequired()) {
            if (SessionManager.isClient(request) ||
                    SessionManager.isAdmin(request)){
                return null;
            } else {
                return mapping.findForward("ShowAuthentication");
            }

        } else {

            return null;

        }

    }
}
